package com.mall.controller;

import com.mall.common.resp.ResponseResult;
import com.mall.entity.Address;
import com.mall.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xyl
 * @date 2021-03-03 15:22
 */
@Api(tags = "用户收货地址接口")
@RestController
@RequestMapping("address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @ApiOperation("添加")
    @PostMapping("add")
    public ResponseResult add(@RequestBody Address address){
        return ResponseResult.toResponse(addressService.add(address));
    }

    @ApiOperation("修改")
    @PostMapping("modify")
    public ResponseResult modify(@RequestBody Address address){
        if(ObjectUtils.isEmpty(address.getAddressId())){
            return ResponseResult.error("id不能为null");
        }
        return ResponseResult.toResponse(addressService.modify(address));
    }

    @ApiOperation("移除")
    @PostMapping("remove")
    public ResponseResult remove(@RequestParam Integer addressId){
        return ResponseResult.toResponse(addressService.remove(addressId));
    }

    @ApiOperation("查询当前用户列表地址")
    @GetMapping("list")
    public List<Address> list(Address address){
        return addressService.list(address);
    }

    @ApiOperation("根据id获取地址信息")
    @GetMapping("get")
    public ResponseResult get(@RequestParam Integer addressId){
        return ResponseResult.success(addressService.get(addressId));
    }
}
